/******************************************
	commandOrdered_aircav.sqf
	Sends an Air Cavalry

	Xealot 2011-03-03
*******************************************/

createGroupMarker = {
	/* Function to create a marker for the infantry group */
	/* Args:
		0 chopper object
	*/

	_chopper = _this select 0;
	_crew = crew _chopper;
	_tmpGrp = _crew - [driver _chopper, gunner _chopper, commander _chopper];

	_tmp = _tmpGrp select 0;
	Add_MarkersGroup =  group _tmp;
	Add_MarkersType = "b_inf";
	Add_Markers = true;

	PublicVariable "Add_MarkersGroup";
	PublicVariable "Add_MarkersType";
	PublicVariable "Add_Markers";	
};

spawnSoldier = {
	/* Function to spawn a soldier and assign him into a group */
	/* Args:
		0 soldier class
		1 group		

	   Returns:
		soldier object
	*/
	_grp = _this select 1;
	_class = _this select 0;

	newSoldier = _grp createUnit [_class, [0,0], [], 100, "FORM"];
	[newSoldier] join _grp; 
	_newSoldier = newSoldier;
	_newSoldier
};

createChopper = {
	/* Function to create chopper and populate it with dudes. Crew and Cargo are separate groups */	
	/* Args:
		0 chopper class
		1 crew class
		2 base position
		3 side SpawnInfArray
		4 side
	   Returns:
		chopper object
	*/

	/* Create chopper */
	veh = createVehicle [_this select 0, _this select 2, [], 100, "FLY"];
	_veh = veh;
	_side = _this select 4;

	/* Create crew */
	_grp = createGroup _side;
	transportPilot = _grp createUnit [_this select 1, position player, [], 100, "FORM"];
	_pilot = transportPilot;
	transportGunner = _grp createUnit [_this select 1, position player, [], 100, "FORM"];

	/* Assign crew to chopper */
	[_pilot, transportGunner] join _grp;
	_pilot moveInDriver _veh;
	transportgunner moveInGunner _veh;
	[_grp] exec "server\deleteGroup.sqs";

	/* Create infantry squad */
	_spawnInfArray = _this select 3;
	_infGrp = createGroup _side;

	/* First create the leader */
	_SpawnSoldier = _spawnInfArray select 0;
	_dude = [_SpawnSoldier, _infGrp] call spawnSoldier;
	_dude moveInCargo _veh;

	/* Create a random mix of 8 soldiers */
	for [{_i=0}, {_i < 8}, {_i = _i + 1}] do {
		_rand = round(random((count _spawnInfArray) - 1));
		if (_rand < 0) then {
			_rand = 0;
		};

		_SpawnSoldier = _spawnInfArray select _rand;
		_dude = [_SpawnSoldier, _infGrp] call spawnSoldier;
		_dude moveInCargo _veh;
	};

	_infGrp setCombatMode "RED";
	[_infGrp] exec "server\deleteGroup.sqs";

	_veh
};


/* Start of script begins here */


_pos = [(_this select 0) select 0, (_this select 0) select 1, 0];

if (!surfaceIsWater [_pos select 0, _pos select 1]) then {

	/* Make sure we arent sending them to the enemy base */
	_checker = nil;
	if (side player == side0) then { _checker = format ["respawn_%1", side1]; };
	if (side player == side1) then { _checker = format ["respawn_%1", side0]; };

	if ([(_pos select 0), (_pos select 1)] distance getMarkerPos _checker > 1000) then {
		_basePos = [getMarkerPos Format["respawn_%1", side player] select 0, getMarkerPos format["respawn_%1", side player] select 1, 0];
		_sideCrew = nil;
		_sideChopper = nil;
		_side = nil;
		_spawnInfArray = nil;
		onMapSingleClick "";	// reset it
		
		if (side player == side0) then {
			_sideCrew = side0_pilot;
			_sideChopper = side0_transportChopper;
			_side = side0; 
			_spawnInfArray = side0_SpawnInfarray;
		};
		if (side player == side1) then {
			_sideCrew = side1_pilot;
			_sideChopper = side1_transportChopper;
			_side = side1;
			_spawnInfArray = side1_SpawnInfarray;
		};


		/* Create first chopper */
		_chopper1 = [_sideChopper, _sideCrew, _basePos, _spawnInfArray, _side] call createChopper;

		/* Create second chopper */
		_chopper2 = [_sideChopper, _sideCrew, _basePos, _spawnInfArray, _side] call createChopper;


		/* Dispatch choppers to assault position - slightly different landing positions to avoid collision */
		[driver _chopper1, _chopper1,_pos,_basepos] exec "player\MoveAndUnload.sqs";

		_rand = 200 - random(400);
		_pos = [(_pos select 0) + _rand, (_pos select 1) + _rand, _pos select 2];

		[driver _chopper2, _chopper2,_pos,_basepos] exec "player\MoveAndUnload.sqs";

		sleep 2;

	
		/* Create markers for both infantry groups */
		[_chopper1] call createGroupMarker;
		[_chopper2] call createGroupMarker;

		/* Script complete - MoveAndUnload handles RTB and vehicle deletion */
	
	} else {
		player groupChat "Aborted - Too close to the enemy airbase.";
	};
} else {
	player groupChat "Can't land there";
};